package com.beilang.hotline.title.page;

import com.beilang.base.codelookup.manager.CodeLookupMgr;
import com.beilang.base.core.page.BasePage;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.CodeConstant;
import com.beilang.base.util.DateUtil;
import com.beilang.hotline.title.manager.TitleMgr;
import com.beilang.hotline.title.model.TitleModel;
import com.beilang.hotline.title.to.SearchTO;
import com.beilang.hotline.user.model.UserModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("unchecked")
public class TitlePage extends BasePage{
	private TitleMgr titleMgr;
	private CodeLookupMgr codeLookupMgr;

	public void setCodeLookupMgr(CodeLookupMgr codeLookupMgr) {
		this.codeLookupMgr = codeLookupMgr;
	}

	public void setTitleMgr(TitleMgr titleMgr) {
		this.titleMgr = titleMgr;
	}

    public List getTitleCodeList(int sysId,int issueId){
        List list=new ArrayList();
        list.add(sysId);
        list.add(issueId);
        return codeLookupMgr.getCodeListWithFilter(CodeConstant.TITLE_LIST, CodeConstant.CODE_FILTER, list);
    }
    public List getTitleCodeListWithEmpty(int sysId,int issueId){
        List list=new ArrayList();
        list.add(sysId);
        list.add(issueId);
        return codeLookupMgr.getCodeListWithFilter(CodeConstant.TITLE_LIST, CodeConstant.CODE_FILTER, list,true);
    }

    public List getSysList(){
		List list=new ArrayList();
		list.add(getEmptyIntCodeModel());
		list.addAll(getAddSysList());
		return list;
	}

	public List getAddSysList(){
        return codeLookupMgr.getCodeListWithFilter(CodeConstant.SYS_NAME, CodeConstant.CODE_FILTER, new ArrayList());
	}

	public List getUpdateSysList(){
        return getAddSysList();
	}    

    public List getIssueList(int sysId){
		List list=new ArrayList();
		list.add(getEmptyIntCodeModel());
		list.addAll(getAddIssueList(sysId));
		return list;
	}

	public List getAddIssueList(int sysId){
        List list=new ArrayList();
        list.add(sysId);
        return codeLookupMgr.getCodeListWithFilter(CodeConstant.ISSUE_LIST, CodeConstant.CODE_FILTER, list);
	}

	public List getUpdateIssueList(int sysId){
        return this.getAddIssueList(sysId);
	}    

    public PaginationHelper getSearchList(PaginationHelper helper,SearchTO searchTO){
		helper=titleMgr.getTitleList(helper, searchTO);
		return helper;
	}

	public PaginationHelper deleteTitleList(PaginationHelper helper,SearchTO searchTO,List deleteList){
        for (Object obj : deleteList) {
            TitleModel titleModel = (TitleModel) obj;
            titleMgr.deleteTitle(titleModel.getTitleId());
        }
		return this.getSearchList(helper, searchTO);
	}

	public TitleModel addTitle(TitleModel titleModel){
		UserModel user=this.getLoginUser();
		titleModel.setCreateId(user.getUserId());
		titleModel.setUpdateId(user.getUserId());
		Date date = DateUtil.getDate();
		titleModel.setCreateTime(date);
		titleModel.setUpdateTime(date);
		titleModel=titleMgr.createTitle(titleModel);
		return titleModel;
	}

	public TitleModel getViewTitle (Long titleId){
		return titleMgr.getTitle(titleId);
	}

	public TitleModel getUpdateTitle (Long titleId){
		return getViewTitle(titleId);
	}

	public TitleModel UpdateTitle(TitleModel titleModel){
		UserModel user=this.getLoginUser();
		titleModel.setUpdateId(user.getUserId());
		titleModel.setUpdateTime(DateUtil.getDate());
		titleModel=titleMgr.saveTitle(titleModel);
		return titleModel;
	}
}
